<?php /* УТФ-8:fix */

class SPImodel__content__manage extends SPImodel__content
{
    public function select( $type, $catId = null, $filter_search = '', $orderby = null)
    {
        switch( $orderby['dir'] )
        {
            default:
            case '+':           $orderby['dir'] = 'ASC';  break;
            case '-':           $orderby['dir'] = 'DESC'; break;
        }

        switch( $orderby['field'] )
        {
            default:
            case 'name':        $q_orderby = "c.name {$orderby['dir']}, c.id {$orderby['dir']}"; break;
            case 'pubdate':     $q_orderby = "c.pubdate {$orderby['dir']}, c.id {$orderby['dir']}"; break;
            case 'order_index': $q_orderby = "c.order_index {$orderby['dir']}, c.id DESC"; break;
            case 'brand':       $q_orderby = "brand {$orderby['dir']}, c.id DESC"; break;
            case 'id':          $q_orderby = "c.id {$orderby['dir']}"; break;
        }

        // --------------------------------------------------------------------

        switch( $type )
        {
            case 'catalog':         $q_type = " ( c.type = '{$type}' ) "; break;
            case 'news':            $q_type = " ( c.type = '{$type}' ) "; break;
            case 'gallery':         $q_type = " ( c.type = '{$type}' ) "; break;
            case 'partners':        $q_type = " ( c.type = '{$type}' ) "; break;
            default:                $q_type = " FALSE "; break;
        }
		
        // --------------------------------------------------------------------

        $store = SPIstore::I();

        $query =    " SELECT c.*, (SELECT name FROM ".SPIcfg::$store['table:brands']." where id = c.brand ) as brand"
               .      " FROM ".SPIcfg::$store['table:content']." as c "
               .     " WHERE TRUE "
			   . ( $catId ? " AND c.pid_catalog = ".(int)$catId." " : '' )
               . ( $q_type ? " AND {$q_type} " : '' )
               . ( $filter_search
                 ? '     AND (  ' . $store->prepare_search_query( 'id', $filter_search )
                 . '         OR ' . $store->prepare_search_query( 'name', $filter_search )
                 . '         OR ' . $store->prepare_search_query( 'anons', $filter_search )
                 . '         OR ' . $store->prepare_search_query( 'description', $filter_search )
                 . '         OR ' . $store->prepare_search_query( 'brand', $filter_search )
                 . '         ) '
                 : ''
                 )
               . " ORDER BY {$q_orderby} "
               ;

        return $store->query( $query );
    }

    public function get_by_id( $item_id )
    {
        $query =    " SELECT *"
               .      " FROM ".SPIcfg::$store['table:content']
               .     " WHERE id=" . (int) $item_id
               ;

        return SPIstore::I()->query( $query )->fetch_and_close();
    }

    public function set_activity( $set_activity_value, $item_id )
    {
        return SPIstore::I()->modify(
            SPIcfg::$store['table:content'],
            array( 'workstatus' => ($set_activity_value == 'on' ? 1 : 0 ) ),
            array( 'id' => $item_id )
        );
    }

    public function insert( $fields )
    {
        $fields['pid_profile_addby'] = SPI::I('Iam')->get_id();
		$fields['t_created'] = strftime('%Y-%m-%d %H:%M:%S');
		
        $result = SPIstore::I()->insert(
            SPIcfg::$store['table:content'],
            $fields,
            null,
            array( 'id' => SPIcfg::$store['sequence:global'] )
        );

        return $result === false ? false : $result['id'];
    }

    public function modify( $item_id, $fields )
    {
        return SPIstore::I()->modify(
            SPIcfg::$store['table:content'],
            $fields,
            array( 'id' => $item_id )
        );
    }

    public function delete( $item_id )
    {
        return SPIstore::I()->delete(
            SPIcfg::$store['table:content'],
            array( 'id' => $item_id )
        );
    }

    public function reset_order_indexes( $type, $cat = null )
    {
        $CONDITION = array();
		$CONDITION['type'] = $type;
		$CONDITION['pid_catalog'] = $cat ? (int)$cat : null ;
		
		return SPIstore::I()->modify(
            SPIcfg::$store['table:content'],
            array( 'order_index'=>-1 ),
            $CONDITION
        );
    }

    public function select_for_sort( $type, $cat = null )
    {
        switch( $type )
        {
            case 'catalog':         $q_type = " ( type = '{$type}' ) "; break;
            default:                $q_type = " FALSE "; break;
        }
		
        $query =    " SELECT * "
               .      " FROM ".SPIcfg::$store['table:content']
               .     " WHERE workstatus=1 "
			   . ( $cat ? " AND pid_catalog = ".(int)$cat." " : '' )
               . ( $q_type ? " AND {$q_type} " : '' )
               . " ORDER BY order_index ASC, id DESC "
               ;

        return SPIstore::I()->query( $query );
    }
}
